package com.jcl.recruit.api.wechat.controller;

import com.jcl.core.resp.ApiResponse;
import com.jcl.core.security.SecurityUtils;
import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.register.service.RegisterResult;
import com.jcl.recruit.register.service.RegisterVo;
import com.jcl.recruit.user.model.User;
import com.jcl.recruit.verfity.service.VerificationService;
import com.jcl.recruit.wechat.service.WechatLoginService;
import com.jcl.recruit.wechat.service.WechatRegisterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName WechatRegisterAndLoginController
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/14 10:21
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/wechat")
public class WechatRegisterController {

    private static final Logger logger = LoggerFactory.getLogger(WechatRegisterController.class);

    @Autowired
    private WechatLoginService wechatLoginService;

    @Autowired
    private WechatRegisterService wechatRegisterService;

    @Autowired
    private VerificationService verificationService;

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 10:35 2018/9/14
     * @Param [entity, response]
     * @return void
     **/
    @RequestMapping("/register/corp")
    public ApiResponse registerCorp(@RequestBody RegisterVo entity, HttpServletResponse response) {

        if(StringUtils.isEmpty(entity.getCorpName())){

            return ApiResponse.error("企业名称不能为空！");
        }
        if(StringUtils.isEmpty(entity.getEmail())){

            return ApiResponse.error("企业邮箱不能为空！");
        }
        if(StringUtils.isEmpty(entity.getVerifyCode())){

            return ApiResponse.error("验证码不能为空！");
        }
        if(StringUtils.isEmpty(entity.getTelephone())){

            return ApiResponse.error("手机号码不能为空！");
        }

        int number = verificationService.validatePhoneRegisterCode(entity.getTelephone(),
                entity.getVerifyCode());
        if(number == 1){
            return ApiResponse.error("验证码不正确!");
        }else if(number == 2){
            return ApiResponse.error("验证码失效！");
        }

        RegisterResult result = wechatRegisterService.corpRegister(entity);

        if(result.hasError()){
            return ApiResponse.error(result.getErrorMsg(), result.getCode());
        }

        //登录
        User user = wechatLoginService.doLogin(result.getUser(), SecurityUtils.FROM_WECHAT);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("token", user.getToken());
        return ApiResponse.success("注册完成！", dataMap);
    }

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 10:35 2018/9/14
     * @Param [entity, response]
     * @return void
     **/
    @RequestMapping("/register/inviteuser")
    public ApiResponse registerUser(@RequestBody RegisterVo entity, HttpServletResponse response) {

        if(StringUtils.isEmpty(entity.getUsername())){

            return ApiResponse.error("用户名称不能为空！");
        }
        if(StringUtils.isEmpty(entity.getEmail())){

            return ApiResponse.error("邮箱不能为空！");
        }

        logger.info("register accessKey is : {}", entity.getAccessKey());
        RegisterResult result = wechatRegisterService.userRegister(entity);
        if(result.hasError()){
            return ApiResponse.error(result.getErrorMsg(), result.getCode());
        }

        //登录
        User user = wechatLoginService.doLogin(result.getUser(), SecurityUtils.FROM_WECHAT);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("token", user.getToken());
        return ApiResponse.success("注册完成！", dataMap);
    }

}
